﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace assignment_1.Models
{
    public class DataRepository
    {
        ContentDBDataContext m_db = new ContentDBDataContext();

        public void SaveUserCanvas(Canva canvas)
        {
            if (canvas != null)
            {
                m_db.Canvas.InsertOnSubmit(canvas);
                m_db.SubmitChanges();
            }
        }

        public IEnumerable<Canva> getCanvasByUserName(String username)
        {
            var result = from item in m_db.Canvas
                         where item.userName == username
                         select item;
            return result;
        }

        public IEnumerable<Canva> getCanvasByID(int id)
        {
            var result = from item in m_db.Canvas
                         where item.canvasID == id
                         select item;
            return result;
        }

        public Canva getSingleCanvasByUd(int id)
        {
            var result = (from item in m_db.Canvas
                         where item.canvasID == id
                         select item).SingleOrDefault();
            return result;          
        }

        public Canva getUnfinishedCanvas(String unknown)
        {
            var result = (from item in m_db.Canvas
                          where item.userName == "Unknown user"
                          select item).SingleOrDefault();
            return result;
        }

        public void updateCanvasUserName(String username, String unknown)
        {
            var result = getUnfinishedCanvas(unknown);
            if (result != null)
            {
                result.userName = username;
                m_db.SubmitChanges();
            }
        }

        public void deleteCanvas(int id)
        {
            var result = getSingleCanvasByUd(id);
            if (result != null)
            {
                m_db.Canvas.DeleteOnSubmit(result);
                m_db.SubmitChanges();
            }
        }
    }
}